﻿#include "text.h"
#include <QDebug>
#include "../include/dxf/dl_codes.h"
namespace Entities
{

Text::Text()
{

}

Text::Text(const Point&p1,double angle,std::string text)
{
    m_p = p1;
    m_text = text;
    m_x=p1.GetX();
    m_y=p1.GetY();
    m_z=p1.GetZ();
    m_angle = angle;
    m_drawableX=0.0;
    m_drawableY=0.0;
    m_drawableZ=0.0;

}

Text::Text(const Text& text)
{
    m_p = text.GetPt();
    m_text = text.GetText();
    m_angle = text.GetAngle();
    m_x = text.GetX();
    m_y = text.GetY();
    m_z = text.GetZ();
    m_drawableX = text.GetDrawableX();
    m_drawableY = text.GetDrawableY();
    m_drawableZ = text.GetDrawableZ();
    SetAttributes(text.GetAttributes());
}
Text::~Text()
{

}
void Text::printInfo(){
    qDebug()<<"text重要数据打印"<<"m_x="<<m_x<<"m_y="<<m_y<<"m_angle="<<m_angle<<"m_drawableX="<<m_drawableX<<"m_drawableY="<<m_drawableY;
}
void Text::Draw(QPainter& painter)
{
    QPen pen;
    QColor rgb(dxfColors[m_attributes.color][0]*255, dxfColors[m_attributes.color][1]*255, dxfColors[m_attributes.color][2]*255);
    pen.setColor(rgb);
    pen.setWidth(m_attributes.line_width);
    pen.setStyle((Qt::PenStyle)m_attributes.getLineType());
    painter.setPen(pen);
    QPointF p1(m_p.GetDrawableX(),m_p.GetDrawableY());
    painter.translate(p1);            // 第1步：变换旋转中心到所绘制文字左下角
    double an = 0;  //弧度角度转换
    if(m_angle<=PI){
        an = - 180/PI * m_angle;
    }else{
        an = 360 - 180/PI * m_angle;
    }
    painter.rotate(an);                    // 第2步： 旋转一定角度
    painter.drawText(0,0, QString::fromStdString(m_text));      // 第3步： 绘制文字
    painter.resetTransform();                  // 第4步： 恢复坐标旋转
}
void Text::writeDxfInfo(DL_Dxf &dxf, DL_WriterA* dw){
    Attributes a = GetAttributes();
    printInfo();
    DL_TextData data(m_x, m_y, m_z,//text location
                         0.0, 0.0, 0.0,//对齐坐标，
                         0, 0,//文本高度和scale
                         0, 0, 0,
                         m_text,//输入文本
                         "Standard",//字体
                         m_angle);//旋转角度
    dxf.writeText(*dw, data, DL_Attributes(a.layer_name, a.color, a.line_width, a.line_type, 1.0));
}
Entity* Text::Clone()
{
    Entity *text = new Text(*this);
    return text;
}
void Text::Transform(double*params,int size)
{
    m_p.Transform(params,size);
}

void Text::Scale(double ratio)
{
    m_p.Scale(ratio);
}

void Text::Transfer(double dx,double dy,double dz)
{
    m_p.Transfer(dx,dy,dz);
}

void Text::Rotate(double angle,double cx,double cy,double cz)
{
    qDebug()<<"MText::Rotate=================>"<<m_drawableX<<m_drawableY<<cx<<cy<<cz;
}
void Text::CorrectCoord(double bx,
                         double by,
                         double bz,
                         double sx,
                         double sy,
                         double sz,
                         double rotaAngle)
{
    m_p.CorrectCoord(bx,by,bz,sx,sy,sz,rotaAngle);
}

}


